home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / font3d10.zip / Array.H next >
C/C++ Source or Header  |  1994-09-15  |  3KB  |  110 lines

  1. //=================================================================================================
  2. //   Array.H
  3. //
  4. //   Copyright (c) 1994 by Todd A. Prater
  5. //   All rights reserved.
  6. //
  7. //-------------------------------------------------------------------------------------------------
  8. //
  9. //   An array class that allows for sizes larger than 64K.  This is only here because it was the
  10. //   simplest way to get large arrays on MSDOS machines.  I'm on the lookout for better ones...
  11. //
  12. //   Constructor:
  13. //
  14. //      ARRAY<T>(ULONG size, ULONG segSize)  Creates an array with 'size' objects of type 'T'.
  15. //                                           'segSize' is the maximum number of bytes that can
  16. //                                           be allocated at one time.  For MSDOS machines this
  17. //                                           is 64K.  The default value of 64000L is somewhat
  18. //                                           arbitrary; I'm not quite sure what a good value
  19. //                                           would be...
  20. //
  21. //   Destructor:
  22. //
  23. //     ~ARRAY(void)                         
  24. //
  25. //   Member functions:
  26. //
  27. //      Size(void)                           Returns the number of objects in the array.                         
  28. //
  29. //   Operators:
  30. //
  31. //      []                                   Array subscript operator.
  32. //
  33. //
  34. //=================================================================================================
  35.  
  36. #ifndef __Array_H__
  37. #define __Array_H__
  38.  
  39.   #include <stddef.h>
  40.   #include <stdlib.h>
  41.   #include <fstream.h>
  42.   #include "Config.H"
  43.  
  44.  
  45.   template <class T>
  46.   class ARRAY
  47.   {
  48.      private: ULONG size;
  49.               ULONG numOfSegs;
  50.               ULONG objsPerSeg;
  51.               T**   segArray;
  52.  
  53.      public:  ARRAY(ULONG _size, ULONG segSize=64000L)
  54.               {
  55.                  ULONG objectSize = sizeof(T);
  56.  
  57.                  size       = _size;
  58.                  objsPerSeg = segSize/objectSize;
  59.                  numOfSegs  = size/objsPerSeg+1;
  60.  
  61.                  ULONG lastSegSize = size%objsPerSeg;
  62.  
  63.                  segArray = new T* [numOfSegs-1];
  64.                  if (segArray==NULL)
  65.                  {
  66.                     cout<<"ERROR:  Out of memory."<<endl;
  67.                     exit(1);
  68.                  }
  69.  
  70.                  for(ULONG i=0;i<numOfSegs-1;i++)
  71.                  {
  72.                     segArray[i] = new T[objsPerSeg-1];
  73.                     if (segArray[i]==NULL)
  74.                     {
  75.                        cout<<"ERROR:  Out of memory."<<endl;
  76.                        exit(1);
  77.                     }
  78.                  }
  79.  
  80.                  segArray[i] = new T[lastSegSize-1];
  81.                  if (segArray[i]==NULL)
  82.                  {
  83.                     cout<<"ERROR:  Out of memory."<<endl;
  84.                     exit(1);
  85.                  }
  86.               }
  87.  
  88.              ~ARRAY(void)
  89.               {
  90.                  for (ULONG i=0;i<numOfSegs;i++)
  91.                     delete segArray[i];
  92.                  delete segArray;
  93.               }
  94.  
  95.               T&  operator [] (ULONG index)
  96.               {
  97.                  if (index >= size )
  98.                  {
  99.                     cout<<"ERROR: Out of range."<<endl;
  100.                     exit(1);
  101.                  }
  102.                  T& tempT = segArray[index/objsPerSeg][index%objsPerSeg];
  103.                  return tempT;
  104.               }
  105.  
  106.               ULONG Size(void) { return size; }
  107.   };
  108.  
  109.  
  110. #endif